var express = require('express');
var express = require('express');
var router = express.Router();
const servicesService = require('../service/servicesService');



router.get('/all', async function (req, res) {
    let { type, value, current, size, typeId, valueId } = req.query;
    let searchObj = {};
    // 使用正则可以实现模糊查询
    if (type) { searchObj[type] = { $regex: value } } else if (typeId) {
        searchObj[typeId] = valueId;
    }
    let data = await servicesService.findAll(searchObj, { current: current || 1, size: size || 5 })
    res.send(data);
})

//增加服务
router.post('/', async function (req, res, next) {
    let data = await servicesService.insert(req.body);
    res.send(data);
});

//修改服务
router.put("/:id", async function (req, res) {
    let id = req.params.id;
    let data = await servicesService.update(id, req.body);
    res.send(data);
});


//删除服务
router.delete("/:id", async function (req, res) {
    let id = req.params.id;
    let data = await servicesService.delete(id);
    res.send(data);
});

module.exports = router;